﻿/**
 * 小人类的构造方法。
 * @param back      背后对象
 * @param drawer    绘图者
 * @param gameBoard 游戏区域
 * @param type      类型
 * @param x, y      左上角坐标
 * @return 小人
 */
function Man(back, drawer, gameBoard, type, x, y) {
    // 创建物体
    const baseGameObject = new Box(back, drawer, gameBoard, type, x, y)

    // 初始化数据
    baseGameObject.mCurrentImage1 = baseGameObject.mImage           // 当前图片1
    baseGameObject.mCurrentImage2 = baseGameObject.mImage2          // 当前图片2
    baseGameObject.mGameBoard = gameBoard                           // 游戏区域
    baseGameObject.mImageLeft1 = new Image()                        // 左图片1
    baseGameObject.mImageLeft2 = new Image()                        // 左图片2
    baseGameObject.mImageRight1 = new Image()                       // 右图片1
    baseGameObject.mImageRight2 = new Image()                       // 右图片2
    baseGameObject.mImageUp1 = new Image()                          // 上图片1
    baseGameObject.mImageUp2 = new Image()                          // 上图片2
    const mTimer = window.setInterval(dancing, Const.TIMER_INIT)    // 定时器

    // 加载所有图片
    baseGameObject.mImageLeft1.src = Const.IMAGE_FILE_PATH + (type * 10 + 2) + Const.IMAGE_FILE_EXT
    baseGameObject.mImageLeft2.src = Const.IMAGE_FILE_PATH + (type * 10 + 3) + Const.IMAGE_FILE_EXT
    baseGameObject.mImageRight1.src = Const.IMAGE_FILE_PATH + (type * 10 + 4) + Const.IMAGE_FILE_EXT
    baseGameObject.mImageRight2.src = Const.IMAGE_FILE_PATH + (type * 10 + 5) + Const.IMAGE_FILE_EXT
    baseGameObject.mImageUp1.src = Const.IMAGE_FILE_PATH + (type * 10 + 6) + Const.IMAGE_FILE_EXT
    baseGameObject.mImageUp2.src = Const.IMAGE_FILE_PATH + (type * 10 + 7) + Const.IMAGE_FILE_EXT

    /**
     * 跳舞。
     */
    function dancing() {
        if (baseGameObject.mCurrentImage == baseGameObject.mCurrentImage1) {
            baseGameObject.mCurrentImage = baseGameObject.mCurrentImage2
        } else {
            baseGameObject.mCurrentImage = baseGameObject.mCurrentImage1
        }
        baseGameObject.mDrawer.drawImage(baseGameObject.mCurrentImage, baseGameObject.mX * Const.GRID_SIZE, baseGameObject.mY * Const.GRID_SIZE)
        baseGameObject.mGameBoard.repaint()
    }

    /**
     * 移动。
     * @param baseGameObject    基本游戏物体
     * @param direction         方向
     * @param moveResult        移动结果
     * @return 被推动的箱子
     */
    baseGameObject.move = function(baseGameObject, direction, moveResult) {
        var destX = baseGameObject.mX, destY = baseGameObject.mY    // 新位置的坐标
    
        // 根据方向，计算新位置的坐标和图片
        switch (direction) {
            case Const.KEY_DOWN:
                destY++
                baseGameObject.mCurrentImage1 = baseGameObject.mImage
                baseGameObject.mCurrentImage2 = baseGameObject.mImage2
                break
            case Const.KEY_LEFT:
                destX--
                baseGameObject.mCurrentImage1 = baseGameObject.mImageLeft1
                baseGameObject.mCurrentImage2 = baseGameObject.mImageLeft2
                break
            case Const.KEY_RIGHT:
                destX++
                baseGameObject.mCurrentImage1 = baseGameObject.mImageRight1
                baseGameObject.mCurrentImage2 = baseGameObject.mImageRight2
                break
            case Const.KEY_UP:
                destY--
                baseGameObject.mCurrentImage1 = baseGameObject.mImageUp1
                baseGameObject.mCurrentImage2 = baseGameObject.mImageUp2
                break
        }
    
        // 重新设置图片
        baseGameObject.mCurrentImage = baseGameObject.mCurrentImage1
    
        // 先移动旁边的物体
        const box = gBaseGameObjects[destY][destX].move(gBaseGameObjects[destY][destX], direction, moveResult)
        if (moveResult.result == _MoveResult.NO_MOVE) {
            return null
        }
    
        // 移动小人
        Box_Move(baseGameObject, direction, moveResult)
    
        return box
    }

    /**
     * 停止跳舞。
     */
    baseGameObject.stopDancing = function() {
        window.clearInterval(mTimer)
    }

    return baseGameObject
}
